package info.androidhive.tabsswipe;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import info.androidhive.tabsswipe.adapter.MyPagerAdapter;


import java.util.ArrayList;

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

    private ViewPager viewPager;
    MyPagerAdapter mPagerAdapter;
    ArrayList<Fragment> pagerItems;
    private ActionBar actionBar;
    private String[] tabs = {"Actions", "My cards"}; //tab titles

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        pagerItems = new ArrayList<Fragment>();
        pagerItems.add(new ActionsFragment());
        pagerItems.add(new MyCardsFragment());
        mPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), pagerItems);
        mPagerAdapter.setPagerItems(pagerItems);
        viewPager.setAdapter(mPagerAdapter);

        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // добавляем табы
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
        }

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {

                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }

    public void updateViewPager() {
        pagerItems = new ArrayList<Fragment>();
        pagerItems.add(new ActionsFragment());
        pagerItems.add( new AddCardFragment());

        mPagerAdapter.setPagerItems(pagerItems);
        ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        viewPager.setAdapter(mPagerAdapter);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.getTabAt(1);
    }

    @Override
    public void onBackPressed() {
        pagerItems = new ArrayList<Fragment>();
        pagerItems.add( new ActionsFragment());
        pagerItems.add(new MyCardsFragment());

        mPagerAdapter.setPagerItems(pagerItems);
        ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        viewPager.setAdapter(mPagerAdapter);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.getTabAt(1);
    }

}
